package com.lp.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.order.OrderImpl;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.domain.pager.PagerImpl;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.controller.BaseControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lp.business.domain.Result;
import com.lp.business.model.Look;
import com.lp.business.model.Member;
import com.lp.business.model.Report;
import com.lp.business.model.SystemConf;
import com.lp.business.service.CollectionService;
import com.lp.business.service.LookService;
import com.lp.business.service.MemberService;
import com.lp.business.service.ReportService;
import com.lp.business.service.SystemConfService;

/**
 * 报告信息
 * 
 * @author lipeng
 * @date 2018年08月22日 下午1:15:45
 */
@RestController
@RequestMapping("/api/report")
public class ReportController extends BaseControllerSupport {

	@Autowired
	private ReportService reportService;
	@Autowired
	private SystemConfService systemConfService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private LookService lookService;
	@Autowired
	private CollectionService collectionService;

	/**
	 * 获取报告总数 <br>
	 */
	@RequestMapping(value = "total", method = { RequestMethod.POST, RequestMethod.GET })
	public Result total(HttpServletRequest request, final HttpServletResponse response) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			Integer total = reportService.getReportCounts();
			return Result.ok().put("data", total);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	/**
	 * 获取报告列表 <br>
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST, RequestMethod.GET })
	public Result list(HttpServletRequest request, final HttpServletResponse response, @RequestParam Map<String,String> params) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				memberId = "";
			}
			String industry = params.get("industry");
			String name = params.get("name");
			
			Report model = new Report();
			model.setStatus(RecordStatus.AVAILABLE);
			if(V.isNotEmpty(industry)) {
				model.setIndustries(industry);
			}
			if(V.isNotEmpty(name)) {
				model.setName(name);
			}
			Order order = new OrderImpl(request);
			Pager pager = new PagerImpl(request);
			List<Map<String, Object>> reports = reportService.findReports(model, order, pager,memberId);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("reports", reports);
			SystemConf systemConf = systemConfService.findByParamKey("resBasePath");
			if(V.isNotEmpty(systemConf)) {
				data.put("basePath", systemConf.getParamValue());
			}
			Integer total = reportService.getReportCounts();
			data.put("total", total);
			return Result.ok().put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	/**
	 * 获取报告明细 <br>
	 */
	@RequestMapping(value = "detail/{id}", method = { RequestMethod.POST, RequestMethod.GET })
	public Result detail(HttpServletRequest request, final HttpServletResponse response, @PathVariable String id) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			if(V.isEmpty(id)) {
				return Result.error("获取报告明细失败");
			}
			Report report = reportService.get(id);
			if(V.isEmpty(report)) {
				return Result.error("获取报告明细失败");
			}
			//查看报告模型
			Look look = new Look();
			look.setMemberId(member.getId());
			look.setReportId(report.getId());
			//是否订阅过或购买过。付费用户
			boolean isPaid = memberService.isPaid(member.getId(), report.getId());
			boolean isSubscribed = memberService.isSubscribed(member.getId(), report.getIndustries());
			if(isPaid||isSubscribed) {//付费用户
				look.setIsFree("0");
			}else {//非付费用户
				int freeLookCount = 10;//免费次数
				SystemConf systemConf = systemConfService.findByParamKey("freeLookCount");
				if(V.isNotEmpty(systemConf)) {
					freeLookCount = Integer.parseInt(systemConf.getParamValue());
				}
				//是否超过免费次数
				Integer count = memberService.freeLookCount(member.getId(), report.getId());
				if(count>=freeLookCount) {
					return Result.error("超过免费查看次数,后续需要付费了");
				}
				look.setIsFree("1");
			}
			//保存查看记录
			lookService.save(look);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("report", report);
			SystemConf basePath = systemConfService.findByParamKey("resBasePath");
			if(V.isNotEmpty(basePath)) {
				data.put("basePath", basePath.getParamValue());
			}
			//
			//是否收藏过
			int isCollection = collectionService.isCollection(member.getId(), report.getId());
			data.put("isCollection", isCollection);
			return Result.ok().put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	
}
